#include "videoslider.h"
#include "qstyle.h"
#include <QMouseEvent>
#include <QTime>
VideoSlider::VideoSlider(QWidget* parent) : QSlider(parent)
{
    m_isPressed = false;
    m_mutex = new QMutex();
}
VideoSlider::~VideoSlider()
{
    delete m_mutex;
}
/*
 * date: 2023/8/15
 * author: brook
 * description: rewrite the mousePressEvent Function, make the slider to move to the MouseEvent positon
 * @param MouseEvent
*/
void VideoSlider::mousePressEvent(QMouseEvent *ev)
{
    int value = QStyle::sliderValueFromPosition(minimum(), maximum(), ev->position().x(), width());
    m_mutex->lock();
    m_isPressed = true;
    m_mutex->unlock();
    setValue(value);
}

/*
 * date: 2023/8/15
 * author: brook
 * description: drag the slider to move to the destination
 * @param MouseEvent
*/
void VideoSlider::mouseMoveEvent(QMouseEvent *ev)
{
    int value = QStyle::sliderValueFromPosition(minimum(), maximum(), ev->position().x(), width());
    setValue(value);
    emit sendTargetAudioVolume(value);
}

void VideoSlider::mouseReleaseEvent(QMouseEvent *ev)
{
    m_mutex->lock();
    int val = value();
    m_isPressed = false;
    emit sendTargetTime(val);
    emit sendTargetAudioVolume(val);
    m_mutex->unlock();
}

void VideoSlider::getseconds(int curseconds)
{
    if(!m_isPressed)
        setValue(curseconds);
    else
        return;
}

void VideoSlider::getAllSeconds(int all_seconds)
{
    setMinimum(0);
    setMaximum(all_seconds);
}
